﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ShortUrl.Web.Controllers
{
    public class SController : Controller
    {
        // GET: S
        public ActionResult Index()
        {
            string errUrl = ESP.Tool.SystemDefinition.SystemSetting.GetAppSettingValue("ErrUrl");
            int maxLength = int.Parse(ESP.Tool.SystemDefinition.SystemSetting.GetAppSettingValue("maxLengthUrl"));
          
            string[] urlArr = ESP.Tool.ESPRequest.GetUrl().Split('/');

            string key = urlArr[urlArr.Length - 1];
            string SourceUrl = Models.ShortUrlModel.GetSourceUrl(key);

            if (string.IsNullOrEmpty(SourceUrl) || SourceUrl.Length > (maxLength * 5))
            {
                SourceUrl = errUrl;
            }

            return Redirect(SourceUrl);
        }
    }
}